package com.demo1.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

// 注解配置访问路径：浏览器通过 http://localhost:8080/demo1/myinfo 访问
@WebServlet("/myinfo")
public class MyInfoServlet extends HttpServlet {
    // 处理GET请求（浏览器默认请求方式）
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 设置响应编码为UTF-8，避免中文乱码（参考文档中“HttpServletResponse响应头设置”）
        resp.setContentType("text/html;charset=UTF-8");

        // 2. 获取输出流，向浏览器输出内容
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<h1>黄亮 24040765</h1>"); // 核心内容
        out.println("</body>");
        out.println("</html>");

        // 3. 关闭输出流
        out.close();
    }

    // 可选：处理POST请求（若后续有表单提交，可复用doGet逻辑）
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
